using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using System.Collections;
using UKVSTS.FieldDefaultValues;

namespace UKVSTS
{
    public partial class WiDefaultValues : Form
    {

        private WorkItemType m_WorkItemType;

        private List<FieldDefaultValue> m_DefaultValues;


        public WiDefaultValues()
        {
            InitializeComponent();
        }

        private void PopulateDefaultValues()
        {
            listFieldDefaultValues.Items.Clear();

            foreach (FieldDefaultValue fdv in m_DefaultValues) {
                AddFieldDefaultValueToListView(fdv);
            }
        }

        private void AddFieldDefaultValueToListView(FieldDefaultValue fdv)
        {
            ListViewItem lvi = new ListViewItem(fdv.FieldName);
            lvi.SubItems.Add(fdv.DefaultValue);
            lvi.Tag = fdv;

            listFieldDefaultValues.Items.Add(lvi);
        }

        public WorkItemType WorkItemType
        {
            get { return m_WorkItemType; }
            set
            {
                ArgTest.IsNotNull(value, "WorkItemType");
                m_WorkItemType = value;

                m_DefaultValues = FieldDefaultValueConfig.Instance.GetDefaultsForWorkItem(m_WorkItemType);

                PopulateDefaultValues();
            }
        }

        private void cmdAdd_Click(object sender, EventArgs e)
        {
            using (FieldDefaultValueEditor dialog = new FieldDefaultValueEditor())
            {
                dialog.SetForNewFieldDefaultValueMode(WorkItemType, m_DefaultValues);

                if (dialog.ShowDialog(this) == DialogResult.OK)
                {
                    AddFieldDefaultValueToListView(dialog.FieldDefaultValue);

                    m_DefaultValues.Add(dialog.FieldDefaultValue);
                }
            }
        }

        private void listFieldDefaultValues_DoubleClick(object sender, EventArgs e)
        {
            EditSelectedItem();
        }

        private void cmdEdit_Click(object sender, EventArgs e)
        {
            EditSelectedItem();
        }

        private void EditSelectedItem ()
        {
            if (listFieldDefaultValues.SelectedItems.Count == 1)
            {
                FieldDefaultValue fdv = listFieldDefaultValues.SelectedItems[0].Tag as FieldDefaultValue;

                if (fdv != null)
                {
                    using (FieldDefaultValueEditor dialog = new FieldDefaultValueEditor())
                    {
                        dialog.SetForEditMode(fdv, m_WorkItemType);

                        if (dialog.ShowDialog(this) == DialogResult.OK)
                        {
                            PopulateDefaultValues();
                        }
                    }
                }
            }
        }

        private void cmdRemove_Click(object sender, EventArgs e)
        {
            if (listFieldDefaultValues.SelectedItems.Count == 1)
            {
                if (m_DefaultValues.Remove((FieldDefaultValue)listFieldDefaultValues.SelectedItems[0].Tag))
                {
                    PopulateDefaultValues();
                }
            }
        }

        private void listFieldDefaultValues_SelectedIndexChanged(object sender, EventArgs e)
        {
            bool enabled = listFieldDefaultValues.SelectedItems.Count > 0 ;

            cmdEdit.Enabled = enabled;
            cmdRemove.Enabled = enabled;
        }

        private void cmdOK_Click(object sender, EventArgs e)
        {
            // Cool, we are all done. Just need to save away our collection of 
            // default values against this project / workitem.
            //
            FieldDefaultValueConfig.Instance.SetDefaultsForWorkItem(m_WorkItemType, m_DefaultValues);
        }

        private void listFieldDefaultValues_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
        {
            bool enable = true;
            if (listFieldDefaultValues.SelectedItems.Count == 0)
            {
                enable = false;
            }
            cmdEdit.Enabled = enable;
            cmdRemove.Enabled = enable;
        }

        private void WiDefaultValues_HelpButtonClicked(object sender, CancelEventArgs e)
        {
            e.Cancel = true;
            System.Diagnostics.Process.Start("http://www.codeplex.com/UKVSTS/Wiki/View.aspx?title=Work%20Item%20Default%20Values%20Editor");
        }

    }
}